﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Security.Cryptography.X509Certificates;
using CommModule.Security;
using System.Security.Cryptography;

namespace CommModule.Messages
{
    public class CertChallengeMessage: Convertible
    {
        public String _cipheredChallenge;

        public CertChallengeMessage() { }

        public CertChallengeMessage(int clientChallenge)
        {
            _cipheredChallenge = clientChallenge.ToString();
            RSACryptoServiceProvider certcopPublic = CertificateUtils.GetCertcopPublicKey();
            _cipheredChallenge = AssimEncryption.Encrypt(clientChallenge.ToString(), certcopPublic);
        }

        public void ConvertToCanonic(XmlWriter xmlWriter)
        {
            xmlWriter.WriteStartElement("CertChallengeMessage");

            if (_cipheredChallenge != null)
            {
                xmlWriter.WriteStartElement("CipheredChallenge");
                xmlWriter.WriteString(_cipheredChallenge);
                xmlWriter.WriteEndElement();
            }

            xmlWriter.WriteEndElement();
        }

        public void ConvertFromCanonic(XmlTextReader xmlTextReader)
        {
            xmlTextReader.ReadStartElement();

            if (xmlTextReader.Name == "CipheredChallenge")
            {
                _cipheredChallenge = xmlTextReader.ReadElementContentAsString();
            }

            xmlTextReader.ReadEndElement();
        }

    }
}
